/**************************************************
MYSQL BRIDGE between MYSQL and ACTIONSCRIPT 3.0
Author: Nirmel Murtic
Version 1.0
***************************************************/

package tools
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	public class Mysql extends Sprite
	{
		// path for service.php
		private var SERVICE_PATH:String = "http://localhost/studentdesktop/service.php";
		//private var SERVICE_PATH:String = "http://www.ksksarajevo.ba/service.php";
		private var service:HTTPService;
        private var _xml:XML;
        private var par:Object;
        private var tool:Tool = new Tool();
        
        // security key (must be equal to key in service.php file)
        private static var MAGIC:String = "trinity";
	 	
	 	// onResult event
	 	public var MYSQL_RESULT:String = "MYSQL_RESULT";
	 	
		public function Mysql(){
			service = new HTTPService();
			service.resultFormat = "xml";
			service.url = SERVICE_PATH;
			_xml = new XML();
			sql = new Object();
		}
		
		public function set serviceUrl(url:String):void{
			service.url = url;
		}
		
		public function set sql(obj:Object):void{
			par = obj;
			par.magic = MAGIC;
		}
		
		public function startService():void{
            var objSend:Object = new Object;
			service.send(par);
			service.method = "POST";
			service.addEventListener(ResultEvent.RESULT, onResult);
		}
		
		public function onResult(e:ResultEvent):void{
			//trace(e.message);
			_xml = XML(e.result);
			if(_xml.result!="FALSE"){
				dispatchEvent(new Event(MYSQL_RESULT));
			} else tool.createAlert("GRESKA", "Greska u konektovanju na bazu...");
		}
		
		public function get xml():XML{
			return _xml;
		}
	}
}